<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
	include('../../includes/classes/core.php');
	$action 	= $_REQUEST['act'];
	$data 		= '';
	$error		= '';
	$start		= $_REQUEST['start'];
    $end		= $_REQUEST['end'];
    $client_id	= $_REQUEST['id'];
	
	switch ($action) {
		
		case 'get_edit_page':
			$client_id		= $_REQUEST['id'];
			$page 			= GetPage($client_id);
			$data			= array('page'	=> $page);
				
			break;
						
	    case 'get_list':
    	    $count = $_REQUEST['count'];
    	    
    	    $rResult = mysql_query("SELECT      `client`.`id`,
												`client`.`name`,
												SUM(IF(`warehouse_detail`.`type` = 2, 0, `cartridge_pricelist`.`price`)) AS sum,
												ROUND((SUM(`sale_transaction_detail`.`product_quantity` * `sale_transaction_detail`.`self_cost`)), 2) AS self,
												ROUND((SUM(IF(`warehouse_detail`.`type` = 2, 0, `cartridge_pricelist`.`price`)) - SUM(sale_transaction_detail.product_quantity * sale_transaction_detail.self_cost)), 2) AS diff
									FROM        `client`
									LEFT JOIN   `client_objects` ON `client`.`id` = `client_objects`.`client_id`
									LEFT JOIN   `a_letter` ON `client_objects`.`id` = `a_letter`.`client_object_id`
									LEFT JOIN   `warehouse` ON `a_letter`.`id` = `warehouse`.`a_letter_id`
									LEFT JOIN   `warehouse_detail` ON `warehouse`.`id` = `warehouse_detail`.`warehouse_id`
									LEFT JOIN   `warehouse_producing` ON `warehouse_detail`.`id` = `warehouse_producing`.`warehouse_detail_id`
									LEFT  JOIN  `sale_transaction_detail` ON `warehouse_producing`.`id` = `sale_transaction_detail`.`warehouse_producing_id`
									RIGHT JOIN  `cartridge_pricelist` ON `client`.`id` = `cartridge_pricelist`.`client_id` AND `warehouse_producing`.`cartridge_part_id` = `cartridge_pricelist`.`parts_type` AND `cartridge_pricelist`.`production_id` = `warehouse_detail`.`prod_id`
									WHERE     	DATE(`a_letter`.`finish_date`)  BETWEEN date('$start') AND date('$end')
									GROUP BY 	`client`.`id`
									");
			
			$data = array(
				"aaData"	=> array()
			);
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					//General output 
					$row[] = $aRow[$i];
				}
				$data['aaData'][] = $row;
			}
	        break;	    
	    default:
	       	$error =  "nullჰფგყ";
	}
	

	$data['error'] = $error;
	
	echo json_encode( $data );
	
	function GetPage($client_id){
		$result	=	 mysql_fetch_array(mysql_query("SELECT	client.`name` as `name`
													FROM	client
													WHERE	client.id = '$client_id'"));
		$data = '
		<div id="dialog-form">
			<fieldset>
				<legend>ძირითადი ინფორმაცია</legend>
				<table width="80%" class="dialog-form-table" cellpadding="10px" >
					<tr align="center">
						<th colspan="2">
							<label for="client_object">კლიენტი  :&nbsp;&nbsp;&nbsp;&nbsp;</label>'.$result['name'].'
						</th>
					</tr>
				</table>
			</fieldset>
			
			<div class="clear"></div>
			<fieldset>
		    	<legend>ბარათები</legend>
				 <div class="produse">
				    <div class="inner-table">
					    <div id="dt_example" class="ex_highlight_row">
					        <div id="container" class="overhead_container">
					        	<div id="button_area">
					        	</div>
					            <div id="dynamic">
					                <table class="display" id="prod_list" >
					                    <thead>
						                        <tr id="datatable_header">
						                            <th>ID</th>
						                            <th style="width: 170px">ბარათის N</th>
						                            <th style="width: 170px">თარიღი</th>
						                            <th style="width: 100%">თანხა</th>
													<th style="width: 100%">თვითღირებულება</th>
													<th style="width: 100%">სხვაობა</th>
						                        </tr>
					                    </thead>
										<thead>
						                        <tr class="search_header">
							                            <th class="colum_hidden">
							                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
							                            </th>
							                            <th>
							                            	<input type="text" name="search_letter" value="ფილტრი" class="search_init" />
							                            </th>
														<th>
															<input type="text" name="search_date" value="ფილტრი" class="search_init" />
														</th>
														<th>
														</th>
							                            <th>
							                            </th>
									 					<th>
							                            </th>
							                     </tr>
					                    </thead>
									
					                    <tfoot>
					                        <tr>
					                            <th>&nbsp;</th>
					                            <th>&nbsp;</th>
												<th><p align="right">ჯამი:<br/>სულ ჯამი:</th>
					                            <th>&nbsp;</th>
												<th>&nbsp;</th>
					                            <th>&nbsp;</th>
					                        </tr>
					                    </tfoot>
					                </table>
					            </div>
					        </div>
					    </div>
					</div>
				</div>
			</fieldset>
		<!-- ID -->
    </div>
	<div class="inner-table">
		<input type="hidden" style="" name="search_id" class="search_init"  id="client_id" value="'.$client_id.'"/>
	</div';
		return $data;
	}
	
?>